package com.bahiaapk.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{

	private static final String DATABASE_NAME = "bahia-apk-db";
	private static final int DATABASE_VERSION = 4;
	
	
	// Database creation sql statement
	private static final String DATABASE_CREATE_TWITTER_USERS = "CREATE TABLE tbl_twitter_users " +
			"(ID INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL , " +
			"name INTEGER ); ";
	
	private static final String DATABASE_CREATE_TWITTER_MESSAGE = "CREATE TABLE tbl_twitter_messages (" +
			"ID INTEGER PRIMARY KEY  NOT NULL , " +
			"image VARCHAR, " +
			"created_at DATETIME, " +
			"message VARCHAR," +
			"name VARCHAR);";
	
	private static final String DATABASE_CREATE_NEWS = "CREATE TABLE tbl_news (" +
			"ID INTEGER PRIMARY KEY  NOT NULL , " +
			"title VARCHAR, " +
			"urlImage VARCHAR, " +
			"date DATETIME, " +
			"content VARCHAR, " +
			"shortContent VARCHAR, " +
			"urlWebSite VARCHAR, " +
			"active INTEGER," +
			"ranking  INTEGER DEFAULT  (0)," +
			"sent INTEGER DEFAULT (0));";
	
	private static final String DATABASE_CREATE_MEDIA = "CREATE TABLE tbl_media (" +
			"ID INTEGER PRIMARY KEY  NOT NULL , " +
			"title VARCHAR, " +
			"urlImage VARCHAR, " +
			"time VARCHAR, " +
			"date DATETIME, " +
			"urlMedia VARCHAR, " +
			"active INTEGER," +
			"ranking  INTEGER DEFAULT  (0)," +
			"sent INTEGER DEFAULT (0)," +
			"typeID INTEGER);";
	
	
	private static final String DATABASE_DROP_TWITTER_USERS = "DROP TABLE IF EXISTS tbl_twitter_users; ";
	private static final String DATABASE_DROP_TWITTER_MESSAGES = "DROP TABLE IF EXISTS tbl_twitter_messages; ";
	private static final String DATABASE_DROP_NEWS = "DROP TABLE IF EXISTS tbl_news; ";
	private static final String DATABASE_DROP_MEDIA = "DROP TABLE IF EXISTS tbl_media; ";

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(DATABASE_CREATE_TWITTER_USERS);
		database.execSQL("	INSERT INTO tbl_twitter_users (name) VALUES ('sigaBahia');  ");
		database.execSQL("	INSERT INTO tbl_twitter_users (name) VALUES ('ecbahia_oficial');  ");
		database.execSQL("	INSERT INTO tbl_twitter_users (name) VALUES ('revoluctricolor');  ");
		database.execSQL(DATABASE_CREATE_TWITTER_MESSAGE);
		database.execSQL(DATABASE_CREATE_NEWS);
		database.execSQL(DATABASE_CREATE_MEDIA);

		
		

	}

	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
		// Not implemented
		database.execSQL(DATABASE_DROP_TWITTER_USERS);  
		database.execSQL(DATABASE_DROP_TWITTER_MESSAGES);  
		database.execSQL(DATABASE_DROP_NEWS);  
		database.execSQL(DATABASE_DROP_MEDIA);  

		
	    this.onCreate(database);  
	}

}

